// author : sb hwang
// date : 2012.05.15
// touch screen control function

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>

#include "touch.h"

#define EVENT_BUF_NUM	64
#define HOMEBTN		101
#define TIMERBTN	102
#define SETTINGBTN	103
#define HELPBTN		104

int event_fd = -1;
static char* g_device=NULL;
int g_mode = 0;
int g_pointX = 0;
int g_pointY = 0;

// touch screen input sensing roof 
void touchControl_main(void *arg)
{
	int i;
	size_t	read_bytes;
	struct input_event event_buf[EVENT_BUF_NUM];
	int exitFlag = 0;

	printf("1 step : g_device available check\n");
	if(g_device == NULL)
	{
		printf("device connect fail.\r\n");
		return NULL;
	}

	printf("2 step : input control roof\n");
	while(1)
	{
		read_bytes = read(event_fd, event_buf, (sizeof(struct input_event)*EVENT_BUF_NUM));

		if(read_bytes < sizeof(struct input_event))
		{
			printf("%s : read error",g_device);
			return 1;
		}

		for(i=0; i<(read_bytes/sizeof(struct input_event));i++)
		{			
			switch(event_buf[i].type)
			{
				case EV_SYN:
					printf("-----------------------------\n");
					break;
				case EV_KEY:
					printf("Button code %d",event_buf[i].code);
					switch(event_buf[i].value)
					{
						case 1:
							printf(" : pressed\n");
							break;

							//release button after point checking
						case 0:
							printf(" : released\n");
							g_mode = coordinateCheck(g_pointX,g_pointY);
							printf("1_ g_mode = %d\n",g_mode);
							break;

						default:
							printf("Unknown : type %d, code %d, value %d", event_buf[i].type, event_buf[i].code, event_buf[i].value);
							break;

					}
					break;

				case EV_ABS:
					switch(event_buf[i].code)
					{
						case ABS_X:
							printf("X pos : %d\n", event_buf[i].value);
							g_pointX = event_buf[i].value;
							//if(event_buf[i].value < 200 && event_buf[i].value > 100)
							//	exitFlag++;
							break;
						case ABS_Y:
							printf("Y pos : %d\n", event_buf[i].value);
							g_pointY = event_buf[i].value;
							//if(event_buf[i].value < 200 && event_buf[i].value > 100)
							//	exitFlag++;
							break;
						case ABS_PRESSURE:
							printf("Pressure : %s\n", (event_buf[i].value == 1)?"yes":"no");
							break;
						default:
							printf("Touch Unknown : type %d, code %d, value %d\n", event_buf[i].type, event_buf[i].code, event_buf[i].value);
							break;
					}

					break;
				default:
					printf("Unkown : type %d, code %d, value %d\n",event_buf[i].type, event_buf[i].code, event_buf[i].value);
					break;
			
			}

		}
		/*
		if(exitFlag == 2){
			g_mode = 1;

			printf("%d",g_mode);
		}
			//break;
		else{
			g_mode = 0;
			exitFlag = 0;		
			printf("%d",g_mode);
		}
		*/
	
	}
	close(event_fd);

	return NULL;
}
int getMode()
{
	return g_mode;
}
// touch screen device connect and handle create.
int createTouchHandle(int argc, char *argv[])
{
	//char *device;
	/*
	if(argc != 2)
	{
		printf("Usage : key or touch\n");
		exit(1);
	}
	
	if(!strncmp("key",argv[1], 3))
	{
		g_device = "/dev/input/event0";
	}
	else if(!strncmp("touch",argv[1],5))
	{
		g_device = "/dev/input/event1";
	}
	else
	{
		printf("device argument error\n");
		return 1;
	}
	if((event_fd = open(g_device, O_RDONLY)) < 0)
	{
		printf("%s : open error",g_device);
		return 1;
	}
	*/

	g_device = "/dev/input/event1";
	if((event_fd = open(g_device, O_RDONLY)) < 0)
	{
		printf("%s : open error",g_device);
		return 1;
	}

	printf("connect device : %s\r\n",g_device);

	return 1;

}

int coordinateCheck(int pos_x, int pos_y)
{
	int nBtn=0;
	//1. position Y check
	if(pos_y > 400 && pos_y <470)
	{
		//2. position X check
		if(pos_x > 10 && pos_x < 80)
			nBtn = HOMEBTN;
		else if(pos_x > 80 && pos_x < 150)
			nBtn = TIMERBTN;
		else if(pos_x > 150 && pos_x < 220)
			nBtn = SETTINGBTN;
		else if(pos_x > 220 && pos_x < 290)
			nBtn = HELPBTN;			
	}
	return nBtn;
}
